package com.lzx.applib.viewpager;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lzx.applib.R;
import com.lzx.applib.adapter.SimpleRecyclePagerAdapter;
import com.lzx.applib.utils.DpSpDip2Px;
import com.lzx.applib.widget.ShapeTextView;

/**
 * Created by Lzx on 16/11/17 0017.
 */
public class DotPagerIndicator extends LinearLayout implements IViewPagerIndicator {

    public static final String TAG = "DotPagerIndicator";

    int dotSelectedRes;
    int dotUnselectedRes;
    int dotSize;
    int dotSpace;

    private ViewPager viewPager;
    private PagerAdapter adapter;
    private View[] views;

    private int curPosition = 0;

    public DotPagerIndicator(Context context) {
        super(context);
    }

    public DotPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        setGravity(Gravity.CENTER);
        setOrientation(HORIZONTAL);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.DotPagerIndicator);
        dotSelectedRes = array.getResourceId(R.styleable.DotPagerIndicator_dotSelectedRes, android.R.color.holo_green_light);
        dotUnselectedRes = array.getResourceId(R.styleable.DotPagerIndicator_dotUnselectedRes, android.R.color.darker_gray);
        dotSize = array.getDimensionPixelOffset(R.styleable.DotPagerIndicator_dotSize, DpSpDip2Px.getInstance().dip2px(8));
        dotSpace = array.getDimensionPixelOffset(R.styleable.DotPagerIndicator_dotSpace, dotSize);
        array.recycle();
    }

    @Override
    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        viewPager.addOnPageChangeListener(pageChangeListener);
        init();
    }

    private void init() {
        removeAllViews();
        if (viewPager == null || viewPager.getAdapter() == null || viewPager.getAdapter().getCount() <= 1) {
            return;
        }

        adapter = viewPager.getAdapter();
        int count = adapter.getCount() - (adapter instanceof SimpleRecyclePagerAdapter ? 2 : 0);
        views = new View[count];
        for (int i=0; i<count; i++) {
            View img = genImageView();
            addView(img);
            views[i] = img;
        }
    }

    public View genImageView() {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(dotSize, dotSize);
        lp.setMargins(dotSpace/2, 0, dotSpace/2, 0);
        ShapeTextView img = new ShapeTextView(getContext());
        img.solidColor = getResources().getColor(dotUnselectedRes);
        img.solidSelectedColor = getResources().getColor(dotSelectedRes);
        img.setLayoutParams(lp);
        img.setCircle();
        img.setSelected(false);
        return img;
    }

    public void onPageUnselected(int position) {
        views[position].setSelected(false);
    }

    public void onPageSelected(int position) {
        views[position].setSelected(true);
        curPosition = position;
    }

    public ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            int realPosition = position;
            if (adapter instanceof SimpleRecyclePagerAdapter) {
                realPosition = Math.min(adapter.getCount()-3, Math.max(0, position-1));
            }
            DotPagerIndicator.this.onPageUnselected(curPosition);
            DotPagerIndicator.this.onPageSelected(realPosition);
        }
    };
}
